/**
 * AtolCD/Spatialytics/widgets/CubeChooser.js
 *
 * 
 *
 * Copyright (C) 2010 AtolCD
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Vincent Plantegenet
 * @since 201003
 **/


dojo.provide("AtolCD.Spatialytics.widgets.CubeChooser");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("dijit.TitlePane");
dojo.require("dijit.form.SimpleTextarea");

dojo.declare("AtolCD.Spatialytics.widgets.CubeChooser",
		[dijit._Widget, dijit._Templated], {

	// title: String
	//	the title label
	title: "Cube",
	content: "",
	first:true,
	templatePath: dojo.moduleUrl("AtolCD.Spatialytics.widgets","templates/CubeChooser.html"),
	widgetsInTemplate: true,
	
	// --- private attributes:
	_solapContext: null,
	_observerFunc: null,
	_olapAjax: null,
	_config2: null,
	_currCellSet: null,
	_hierarchy : [],
	
	// -- postCreate function
	postCreate: function () {
		var that = this;
		that.first = true;
		var itemsJson = {
			identifier: 'id',
			label: 'Cube',
			items: []};
			
		var title= "Cube";
		var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
		this.cubeChWidget.attr('title', title);
		this.cubeWidget.attr('searchAttr', 'label');
		this.cubeWidget.attr('store', dataStore);
		this.cubeWidget.attr('onChange',
				function() {
					that._handleChange();
				}
		);
		this.inherited(arguments);
	},
	

	// --- private methods
	_handleChange: function () {
	
		this._resetAll();
	},
	
	_resetAll: function () {
		var item = this._getCurrentItemAttr();
		if (item != null) {
			this._solapContext.curr_cube = item.id;
			this._solapContext.wids[0].initialize(this._solapContext.config);
			this._solapContext.wids[1].initialize(this._solapContext.config);
			this._solapContext.wids[1].setCurrentItemAttr(0);
		}
	},

	// --- getters/setters
	
	_setTitleAttr: function (/*String*/ title) {
		this.title = title;
		this.cubeChWidget.attr('title', title);
	},

	_getCurrentItemAttr: function () {
		return this.cubeWidget.attr('item');
	},
	// --- public methods
	
	addMeasure: function (/*integer*/ id,
	/*String*/ label) {
		
		//Mise a jour des valeurs des mesures
		this.cubeWidget.attr('store').newItem({id: id, label: label});
		this._hierarchy[id] = label;
		
		if(id == 0) {
			// first MapStyle added, set the current item in the FilteringSelect
			// with the added MapStyle
			this.cubeWidget.attr('value', 0);
			this._solapContext.curr_cube = this.cubeWidget.attr('item').id;
		}

	},
	
	attachSOLAPContext: function (/*GeoSOA.Spatialytics.SOLAPContext*/ solapContext) {
		this._solapContext = solapContext;
	},
	
	detachSOLAPContext: function () {
		if (this._solapContext != null) {
			this._solapContext = null;
		}
	},

	attachOlapAjax: function (/*GeoSOA.olap4js.OlapAjax*/ olapAjax) {
		this._olapAjax = olapAjax;
	}

});
